package com.yuelife.service.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeOrderinfoSyncModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeOrderinfoSyncRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeOrderinfoSyncResponse;
import com.yuelife.service.config.AlipayConfig;
import com.yuelife.service.pojo.SOrder;
import com.yuelife.service.service.AlipayService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * @author zhaoliancan
 * @description
 * @create 2019-08-08 18:49
 */
@Service
public class AlipayServiceImpl implements AlipayService {



    @Override
    public String aliPay(SOrder sOrder) throws AlipayApiException {
////        //设置响应格式编码
////        response.setContentType("text/html;charset=utf-8");
////
////        PrintWriter out = response.getWriter();
//        //获得初始化的AlipayClient
//        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
//        //设置请求参数
//        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
//        //aliPayRequest.setReturnUrl(AlipayConfig.return_url);
//        //aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);
//
//        //商户订单号，后台可以写一个工具类生成一个订单号，必填
//        String order_number = new String(mOrder.getOrderSn());
//        System.out.println("我是订单号："+order_number);
//        //付款金额，从前台获取，必填
//        String total_amount = new String(mOrder.getOrderPrice()+"");
//        //订单名称，必填
//        String subject = new String(mOrder.getProductName());
//        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
//                + "\"total_amount\":\"" + total_amount + "\","
//                + "\"subject\":\"" + subject + "\","
//                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//        //请求
//        String result = null;
//        try {
//            result = alipayClient.pageExecute(aliPayRequest).getBody();
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        //输出
////        out.println(result);
//        return result;




        //第二种方式，上面是得到form表单的形式
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
        AlipayTradeOrderinfoSyncRequest request = new AlipayTradeOrderinfoSyncRequest();
        AlipayTradeOrderinfoSyncModel model = new AlipayTradeOrderinfoSyncModel();

        model.setBizType("CREDIT_AUTH");
        //
        model.setTradeNo(String.valueOf(sOrder.getOrderId()));
        model.setOutRequestNo(String.valueOf(sOrder.getOrderId()));
        model.setOrderBizInfo("{\"status\":\"COMPLETE\"}");

        request.setBizModel(model);

        AlipayTradeOrderinfoSyncResponse response = alipayClient.sdkExecute(request);
        System.out.println("response: {}" + response.getBody());
//        Assert.assertTrue(response.isSuccess());
        System.out.println(response.isSuccess());

        return response.getBody();
    }

    @Override
    public void zfbOrderPay(HttpServletResponse response, HttpServletRequest request, SOrder sOrder)throws IOException,AlipayApiException {
        System.out.println(sOrder);
        System.out.println("我是支付");
        //设置编码
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
        //设置请求参数
        AlipayTradeWapPayRequest aliPayRequest = new AlipayTradeWapPayRequest();
        aliPayRequest.setReturnUrl(AlipayConfig.return_url); //AlipayConfig.return_url
        aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);//AlipayConfig.notify_url

//        //商户订单号，后台可以写一个工具类生成一个订单号，必填
//        String order_number = new String(mOrder.getOrderSn());
//        //付款金额，从前台获取，必填
//        String total_amount = new String(String.valueOf(mOrder.getOrderPrice()));
//        //订单名称，必填
//        String subject = new String(mOrder.getProductName());
//        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
//                + "\"total_amount\":\"" + total_amount + "\","
//                + "\"subject\":\"" + subject + "\","
//                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//        //请求
//        String result = alipayClient.pageExecute(aliPayRequest).getBody();
//        System.out.println(result);
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        //
        model.setOutTradeNo(String.valueOf(sOrder.getOrderId()));//设置商家订单号
        model.setTotalAmount(String.valueOf(sOrder.getPayAmount()));//设置支付金额
        model.setSubject("你好");//设置地址

        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        aliPayRequest.setBizModel(model);

        String result = alipayClient.pageExecute(aliPayRequest).getBody();
        System.out.println(result);
        //输出
      out.println(result);//直接打开支付宝支付
//        return result;
    }
}

